class MoveGan extends eui.Component {
    constructor() {
        super();
        this.skinName = "MoveganBgSkin";
    }

    public isbai: number = 0;//确认母球

    public MyRange: eui.Image; //桌子可触摸范围
    /**0不摆球，1摆球，2自由球 */
    public isBaiqiu: number = 0;
    public isOwnGame: boolean = false;//是否为游戏所有者
    public isMove: boolean = false;
    private isBegin: boolean = false;//开始游戏？
    /**0 选择界面 1联机对战 2单机游戏 */
    private isLianxi: number = 0;
    /**0,空挡，1，是读条，2，是运行 */
    private isLoading: number = 0;
    private ganpoint: egret.Point;
    private belowenergy: eui.Image;
    /**最大力量 (能量条最大高度) */
    private MaxPower: number = 400;
    /**微调 NO.1*/
    public slipgroup: eui.Group;//微调组
    private slip: eui.Image;
    private slipsize: number = 0;//改变的大小
    private startnum: number = 0;//记录第一次点击Y
    private istouchsp: boolean = false;//是否微调
    /**力度 NO.2*/
    public ld: eui.Group;        //力度组
    private lidudi: eui.Image;//底板
    private liduLabel: eui.Label;//显示力量值
    private StrengthArm: eui.Image;//击球力度球杆
    private energy: eui.Image;//力量条
    private long: number = 0;  //力度文本
    private LastY: number;
    /**杆 NO.3*/
    private ganImg: eui.Image;//球杆
    private angleSpeed: number = 0;//球杆的角度
    private angleNum: number = 0;//球杆角度所在的象限
    private ganLen: number = 581;//球杆图片长度（像素）
    private curVx: number = 0;
    private curVy: number = 0;
    private moveTime: number = 0;
    private huadongAngle: number = 0;
    /**球 NO.4*/
    public AllGroup: eui.Group;
    public ballArr: any[] = [];//所有3D画布上的球 白球0 实心1~7 花色9~15 黑八8 共16颗
    private _r: number = 0;
    public curBall: NewBall = null;//白球
    private otherX: number = 0;//白球摆放起始点
    private otherY: number = 0;//白球摆放起始点
    private posArr: any[] = [];//球洞坐标信息
    private centerY: number = 0;//球桌中心y
    private centerX: number = 0;//球桌中心x
    private tableX: number = 0;//桌子x
    private tableY: number = 0;//桌子y
    private tableW: number = 0;//桌长
    private tableH: number = 0;//桌宽
    //调整边界
    private moveX: number = 0;
    private moveY: number = 0;
    private tableBG: eui.Image;
    private static instance: MoveGan;
    private holeR: number = 28;//球洞半径 
    private bounce: number = -0.7
    public static getInstance(): MoveGan {
        if (this.instance == null) {
            this.instance = new MoveGan();
        }
        return this.instance;
    }
    protected createChildren(): void {
        super.createChildren();
        this.Slip();
        this.touchImage();//点击透明图片定位置
        this.addGanEvt();

        this.ganImg = new eui.Image("gan");
        this.ganImg.anchorOffsetX = 12;

        this.ganImg.x = 250;
        this.ganImg.y = 200;
        this.ganImg.visible = false;
        this.addChild(this.ganImg);

        this.tableX = this.tableBG.x + this.moveX;
        this.tableY = this.tableBG.y + this.moveY;
        this.tableW = this.tableBG.width;
        this.tableH = this.tableBG.height;
        this.posArr = [
            {
                x1: this.tableX + 184,
                y1: this.tableY + 146,
                x2: this.tableX + 144,
                y2: this.tableY + 180,
                hx: this.tableX + 160,
                hy: this.tableY + 155,
            },
            {
                x1: this.tableX + 613,
                y1: this.tableY + 146,
                x2: this.tableX + 666,
                y2: this.tableY + 146,
                hx: this.tableX + 640,
                hy: this.tableY + 130,
            },
            {
                x1: this.tableX + 1091,
                y1: this.tableY + 151,
                x2: this.tableX + 1129,
                y2: this.tableY + 185,
                hx: this.tableX + 1122,
                hy: this.tableY + 154,
            },
            {
                x1: this.tableX + 146,
                y1: this.tableY + 623,
                x2: this.tableX + 184,
                y2: this.tableY + 666,
                hx: this.tableX + 157,
                hy: this.tableY + 650,
            },
            {
                x1: this.tableX + 613,
                y1: this.tableY + 666,
                x2: this.tableX + 666,
                y2: this.tableY + 666,
                hx: this.tableX + 640,
                hy: this.tableY + 672,
            },
            {
                x1: this.tableX + 1129,
                y1: this.tableY + 616,
                x2: this.tableX + 1091,
                y2: this.tableY + 656,
                hx: this.tableX + 1122,
                hy: this.tableY + 648,

            }
        ];
        this.centerX = this.posArr[0].hx + (this.posArr[2].hx - this.posArr[0].hx) * 0.5;
        this.centerY = this.posArr[0].hy + (this.posArr[3].hy - this.posArr[0].hy) / 2;



        document.addEventListener("keydown", function (e: any) {
            if (e.keyCode == 13) {//空格
                MoveGan.getInstance().isbai++;
            }
        });

        this._r = 17;//this.ballArr[0].radius;

        egret.setTimeout(this.startGame1, this, 4000);
    }
    /**开始摆球*/
    public startGame1() {
        var standardpos = [0, 1, 12, 3, 9, 14, 4, 11, 5, 10, 13, 2, 15, 7, 6, 8];
        let ballTemp = [];//彩球
        this.curBall = new NewBall(0);
        this.curBall.nextY = this.curBall.lastY;
        this.curBall.lastX = 550;
        this.curBall.lastY = 550;
        this.curBall.nextX = 550;
        this.curBall.nextY = 550;
        this.curBall.name = "cur";
        this.ballArr.push(this.curBall);
        this.curBall.AllBall
        this.otherX = this.posArr[0].hx + (this.posArr[2].hx - this.posArr[0].hx) * 0.7;
        this.otherY = this.posArr[0].hy + (this.posArr[3].hy - this.posArr[0].hy) / 2;

        for (var i: number = 0; i < 5; i++) {

            var ball: NewBall = new NewBall(this.ballArr.length);
            ball.lastX = this.otherX + (2 * ball.radius + 1) * i * Math.cos(Math.PI / 6);
            ball.lastY = this.otherY - (2 * ball.radius + 1) * i * Math.sin(Math.PI / 6);
            ball.nextX = ball.lastX;
            ball.nextY = ball.lastY;
            this.ballArr.push(ball);
            for (var j: number = 1; j <= i; j++) {
                var ball1: NewBall = new NewBall(this.ballArr.length);
                ball1.lastX = ball.lastX;
                ball1.lastY = ball.lastY + 2 * ball1.radius * j + j;
                ball1.nextX = ball1.lastX;
                ball1.nextY = ball1.lastY;
                this.ballArr.push(ball1);
            }
        }
        for (var i = 0; i < this.ballArr.length; i++) {
            let ball1t = { lastX: 0, lastY: 0, nextX: 0, nextY: 0 };

            ball1t.lastX = this.ballArr[i].lastX;
            ball1t.lastY = this.ballArr[i].lastY;
            ball1t.nextX = this.ballArr[i].nextX;
            ball1t.nextY = this.ballArr[i].nextY;
            ballTemp.push(ball1t);
        }
        for (var i = 0; i < this.ballArr.length; i++) {
            let logNum = standardpos[i];
            this.swtichPosition(this.ballArr[i], ballTemp[logNum]);
        }


        for (var i: number = 0; i < this.ballArr.length; i++) {
            var ball: NewBall = this.ballArr[i];
            this.AllGroup.$children[i].x = ball.nextX;
            this.AllGroup.$children[i].y = ball.nextY;
            // setBallPos1(ball.nextX, ball.nextY, i);

            //console.log(this.ballArr[i],"log");
        }
        // refreshUI();
        this.MyRange.touchEnabled = true;
        /**摆球结束执行自由球代码 */
        // this.addEventListener(egret.Event.ENTER_FRAME, this.showWhiteBall, this);
    }
    public swtichPosition(ball1, ball2) {
        ball1.lastX = ball2.lastX;
        ball1.lastY = ball2.lastY;
        ball1.nextX = ball2.nextX;
        ball1.nextY = ball2.nextY;
    }
    /**
     * 微调
     */
    private Slip() {
        // var long = 751 * 0.7
        // var rect: egret.Rectangle = new egret.Rectangle(12, 0, 68, 412);
        var shape: egret.Shape = new egret.Shape();
        shape.graphics.beginFill(0x00ff00);
        shape.graphics.drawRoundRect(40, -7, 70, 400, 100, 100);
        shape.graphics.endFill();
        shape.alpha = 0;
        this.slipgroup.addChild(shape);
        shape.touchEnabled = false;
        this.slip.mask = shape;
        this.slip.touchEnabled = true;
        this.slip.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.startime, this);
        this.slip.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouch, this);
        this.slip.addEventListener(egret.TouchEvent.TOUCH_END, this.notouchsp, this);
    }
    private notouchsp(evt: egret.TouchEvent) {//结束微调
        this.istouchsp = false;
    }
    private XX: number = 0;
    private YY: number = 0;
    private startime(evt: egret.TouchEvent) {
        this.startnum = evt.stageY;
    }
    private onTouch(evt: egret.TouchEvent) {
        this.istouchsp = true;
        if (evt.stageY > this.startnum) {//下滑
            this.slip.y += 1.5;
            this.slipsize += 0.15;     //杆下移
        } else if (evt.stageY < this.startnum) {//上滑
            this.slip.y -= 1.5;
            this.slipsize -= 0.15;     //杆上移
        }
        //记录本次 evt.stageY 值赋给startnum 下次判断使用
        this.startnum = evt.stageY
        if (this.slip.y >= 0) {
            this.slip.y -= 199;
        }
        else if (this.slip.y <= -400) {
            this.slip.y += 156;
        }
        // 当实现功能前把鼠标X，Y定死只改变差值（定值XX，YY）
        if (this.istouchsp && !this.isMove) {
            this.moveGan(this.XX, this.YY);
        }

        //egret.setTimeout(() => { },this, 100);//延时执行
    }


    /**击球控制 */
    private addGanEvt() {
        this.StrengthArm.touchEnabled = false;
        this.energy.touchEnabled = false;
        this.belowenergy.visible = false;
        this.belowenergy.touchEnabled = false;
        this.energy.height = 0;
        this.long = 0;

        this.UpdateLiduLabel();
        var rectSA = new egret.Rectangle(0, 0, this.StrengthArm.width, 380);
        this.StrengthArm.mask = rectSA;
        this.belowenergy.y = this.energy.height + 48;
        this.lidudi.addEventListener(egret.TouchEvent.TOUCH_BEGIN, e => {
            this.LastY = e.stageY;
            this.ganpoint = new egret.Point(this.ganImg.x, this.ganImg.y);
            // this.slip.touchEnabled = false;
            // this.MyRange.touchEnabled = false;
        }, this);


        //拉动
        this.lidudi.addEventListener(egret.TouchEvent.TOUCH_MOVE, e => {

            // if (this.isOwnGame && (this.isBaiqiu == 0) && !this.isMove) {
            var c = e.stageY - this.LastY;

            //修改位置移动
            this.energy.height += c;
            this.long = this.energy.height;
            this.belowenergy.y = this.energy.height + 48;

            if (this.long < 30) {//大于最小值
                this.belowenergy.visible = false;
            }
            else if (this.long > this.MaxPower - 30) {//小于最大值

                this.belowenergy.visible = false;
            }
            else {//显示范围
                this.belowenergy.visible = true;
            }



            var isneedmove: boolean = true;
            if (this.long < 2) {//最低

                this.energy.height = 0;
                this.long = 0;

                isneedmove = false;
            }

            if (this.long > this.MaxPower - 2) {//最高

                this.energy.height = this.MaxPower;
                this.long = this.MaxPower;

                isneedmove = false;
            }
            this.LastY = e.stageY;
            this.UpdateLiduLabel();

            let tw = egret.Tween.get(this.ganImg);

            if (isneedmove) {
                var oldx: number = this.ganImg.x;
                var oldy: number = this.ganImg.y;
                //球下一帧的移动坐标
                var dx: number = this.ballArr[this.isbai].nextX - oldx;
                var dy: number = this.ballArr[this.isbai].nextY - oldy;
                //球的弧度（反映球的移动方向,画布坐标系）
                var angle: number = Math.atan2(dy, dx);
                var sin: number = Math.sin(angle);
                var cos: number = Math.cos(angle);
                var pos0: egret.Point = new egret.Point(0, 0);
                pos0.x -= c;
                var pos0F: any = this.rotateR(pos0.x, pos0.y, sin, cos, false);
                var newOldx = oldx + pos0F.x;
                var newOldy = oldy + pos0F.y;
                tw.to({ x: newOldx, y: newOldy });
                this.LastY = e.stageY;
            }
            // }
        }, this);
        //归位
        function e() {
            this.energy.height = 0;
            this.endFunc();
            this.UpdateLiduLabel();
            this.slip.touchEnabled = true;
        };
        this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, e, this);
        this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, e, this);
        this.addEventListener(egret.TouchEvent.TOUCH_END, e, this);//ok
    }

    /**更新力量显示 */
    private UpdateLiduLabel(): void {
        if (this.long < 0) this.long = 0;
        if (this.long > this.MaxPower) this.long = this.MaxPower;
        var t = parseInt((this.long * 100 / this.MaxPower).toString());
        this.liduLabel.text = "" + t + "";
    }


    private rotateR(x: number, y: number, sin: number, cos: number, reverse: Boolean): egret.Point {
        var result: egret.Point = new egret.Point();
        if (reverse) {
            result.x = x * cos + y * sin;
            result.y = y * cos - x * sin;
        } else {
            result.x = x * cos - y * sin;
            result.y = y * cos + x * sin;
        }
        return result;
    }


    private endFunc() {
        if (this.long < 1) {
            return;
        }
        // if (!this.isMove && this.isBegin) 
        //    {
        //确定击球的力度
        var speed: number = 2 * this.long / this.MaxPower;
        //判断发射角度象限
        if (this.angleNum == 1) {
            this.curVx = speed * Math.sin(this.angleSpeed);
            this.curVy = -speed * Math.cos(this.angleSpeed);
        }
        else if (this.angleNum == 2) {
            this.curVx = speed * Math.cos(this.angleSpeed);
            this.curVy = speed * Math.sin(this.angleSpeed);
        }
        else if (this.angleNum == 3) {
            this.curVx = speed * Math.sin(this.angleSpeed);
            this.curVy = -speed * Math.cos(this.angleSpeed);
        }
        else if (this.angleNum == 4) {
            this.curVx = -speed * Math.cos(this.angleSpeed);
            this.curVy = speed * Math.sin(this.angleSpeed);
        }
        // this.ClearAllLine();//清除瞄准线
        //给白球加速度
        this.ballArr[this.isbai].vx = this.curVx;
        this.ballArr[this.isbai].vy = this.curVy;
        //结束我的回合 球开始移动 我的游戏结束
        this.isOwnGame = false;
        this.isMove = true;
        this.isBegin = false;
        this.isLoading = 0;
        //球上一帧的坐标
        var oldx: number = this.ganImg.x;
        var oldy: number = this.ganImg.y;
        //球下一帧的移动坐标
        var dx: number = this.ballArr[this.isbai].nextX - oldx;
        var dy: number = this.ballArr[this.isbai].nextY - oldy;
        //球的弧度（反映球的移动方向,画布坐标系）
        var angle: number = Math.atan2(dy, dx);
        var sin: number = Math.sin(angle);
        var cos: number = Math.cos(angle);

        var pos0: egret.Point = new egret.Point(0, 0);
        pos0.x -= 80 * this.long / this.MaxPower;
        var pos0F: any = this.rotateR(pos0.x, pos0.y, sin, cos, false);

        var newOldx = oldx + pos0F.x;
        var newOldy = oldy + pos0F.y;
        //开始播放杆击球的动画
        var tw = egret.Tween.get(this.ganImg);
        tw.to({ x: newOldx, y: newOldy }, 10 * (1 - this.long / this.MaxPower));
        tw.to({ x: oldx, y: oldy }, 6 * (1 - this.long / this.MaxPower));
        tw.call(function () {
            this.isLoading = 2;
            this.ganImg.visible = false;
            this.long = 0;
            this.UpdateLiduLabel();
            var v = Math.sqrt(this.curVx * this.curVx + this.curVy * this.curVy) / 2;
            // this.PlaySoundWithVolume(v, TableUI.Instance().shootSound);
            this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
        }, this)
        // this.ld.visible = false;
        // this.slipgroup.visible = false;
        //  }

    }
    private enterFrame() {
        if (this.isLoading == 2) {
            this.updataMask();
            // refreshUI();
        }
    }

    public moveGan(mPX: number, mPY: number) {

        // if (this.isBaiqiu != 0) return;
        //console.log(this.ganImg.rotation, "杆的旋转");
        // console.log("鼠标X", mPX, "鼠标Y", mPY);
        // console.log("细改变的值", this.slipsize);
        if ((this.ganImg.rotation > -45 && this.ganImg.rotation < 45) ||
            (this.ganImg.rotation < -135 && this.ganImg.rotation > -180) ||
            ((this.ganImg.rotation > 135 && this.ganImg.rotation < 180))) { //横杆
            mPY += this.slipsize;
        } else {//竖杆
            mPX += this.slipsize;
        }
        // if (mPX >= 116 && mPY >= 31 && mPX <= 1311 && mPY <= 683) {
        var dx: number = mPX - this.ballArr[this.isbai].nextX;
        var dy: number = mPY - this.ballArr[this.isbai].nextY;
        var dist: number = Math.sqrt(dx * dx + dy * dy);
        if (dist <= this._r) {
            return;
        }
        //显示球杆的旋转
        this.showGan(mPX, mPY, this.ballArr[this.isbai].nextX, this.ballArr[this.isbai].nextY)
        //  this.showGan(mPX, mPY,400,400); 实例
        setline.getinstance().setSubline(this._r, this.angleSpeed, this.angleNum, this.huadongAngle, 28, this.posArr, this.curBall, this.ballArr);
        //this.setSubline1();
        if (this.isLianxi == 1) {
            var curTime: number = egret.getTimer();
            if (curTime - this.moveTime > 100) {
                this.moveTime = curTime;
                var moveGanData = {
                    point: { x: mPX, y: mPY },
                    angle: this.ganImg.rotation
                }
            }
            // }

        }
    }

    /**显示杆的移动 */
    public showGan(_x: number, _y: number, _bx: number, _by: number) {
        // public showGan(_x: number, _y: number) {
        // var qx: number = this.ballArr[this.isbai].nextX;//01
        // var qy: number = this.ballArr[this.isbai].nextY;//02
        // console.log(_bx,_by,"母球XY");
        var qx: number = _bx;
        var qy: number = _by;


        var dx: number = _x;
        var dy: number = _y;

        var _a: number = dx - qx;
        var _b: number = dy - qy;
        var _c: number = Math.sqrt((dx - qx) * (dx - qx) + (dy - qy) * (dy - qy));

        var angle: number = 0;
        if (_a >= 0 && _b <= 0) {
            angle = Math.asin(_a / _c) * 180 / Math.PI - 90;
            this.angleSpeed = Math.asin(_a / _c);
            this.angleNum = 1;
            this.huadongAngle = this.angleSpeed - Math.PI / 2;
            this.ganImg.x = qx - (this.ganLen) * Math.sin(Math.asin(_a / _c));
            this.ganImg.y = qy + (this.ganLen) * Math.cos(Math.asin(_a / _c));
        }
        if (_a > 0 && _b > 0) {
            angle = (Math.asin(_b / _c)) * 180 / Math.PI;
            this.angleSpeed = Math.asin(_b / _c);
            this.angleNum = 2;
            this.huadongAngle = this.angleSpeed;
            this.ganImg.x = qx - (this.ganLen) * Math.cos((Math.asin(_b / _c)));
            this.ganImg.y = qy - (this.ganLen) * Math.sin((Math.asin(_b / _c)));
        }
        if (_b < 0 && _a < 0) {
            angle = Math.asin(_a / _c) * 180 / Math.PI - 90;
            this.angleSpeed = Math.asin(_a / _c);
            this.angleNum = 3;
            this.huadongAngle = this.angleSpeed - Math.PI / 2;
            this.ganImg.x = qx + (this.ganLen) * Math.sin(Math.asin(-_a / _c));
            this.ganImg.y = qy + (this.ganLen) * Math.cos(Math.asin(-_a / _c));
        }
        if (_a < 0 && _b > 0) {
            angle = (-(Math.asin(_b / _c) + 90 * Math.PI / 180)) * 180 / Math.PI - 90;
            this.angleSpeed = Math.asin(_b / _c);//球杆的角度
            this.angleNum = 4;
            this.huadongAngle = Math.PI - this.angleSpeed;
            this.ganImg.x = qx + (this.ganLen) * Math.cos(Math.asin(_b / _c));
            this.ganImg.y = qy - (this.ganLen) * Math.sin(Math.asin(_b / _c));
        }
        this.ganImg.rotation = angle;//改变度数
        this.ganImg.visible = true;

    }

    /**
      * 点击图片定位置
      */
    private touchImage() {
        this.MyRange.touchEnabled = true;
        this.MyRange.addEventListener(egret.TouchEvent.TOUCH_TAP, this.startMyRange, this);
        this.MyRange.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.moveMyRange, this);
    }
    private startMyRange(evt: egret.TouchEvent) {
        if (this.isBaiqiu == 0) {
            this.XX = evt.stageX;
            this.YY = evt.stageY;
            this.moveGan(evt.stageX, evt.stageY);
        }
    }
    private moveMyRange(evt: egret.TouchEvent) {
        if (this.isBaiqiu == 0) {
            this.XX = evt.stageX;
            this.YY = evt.stageY;
            this.moveGan(evt.stageX, evt.stageY);
        }
    }


    private updataMask() {
        for (var u: number = 0; u < 30; u++) {
            var len: number = this.ballArr.length;//所有球的数组
            var num: number = 0;//已静止的球
            /**所有球 */
            for (var i: number = len - 1; i >= 0; i--) {
                var ball: NewBall = this.ballArr[i];
                if (ball.vx == 0 && ball.vy == 0 && ball.isJindong == -1 || ball.isJindong == -2) {
                    num++;
                }
                if (ball.isJindong != -1 && ball.isJindong != -2) {

                    //绕洞旋转效果
                    var pt: egret.Point = egret.Point.polar(11, ball.degree * Math.PI / 180);
                    ball.nextX = this.posArr[ball.isJindong].hx + pt.x;
                    ball.nextY = this.posArr[ball.isJindong].hy + pt.y;
                    ball.degree += 0.6;

                    if (ball.degree >= 300 && ball.degree < 300.6) {
                        //绕洞旋转结束
                    }//进球结束
                }
                else {
                    if (ball.isJindong != -2) {
                        if (ball.vx != 0 || ball.vy != 0) {
                            ball.nextX += ball.vx;
                            ball.nextY += ball.vy;
                            ball.startRoll();
                            this.checkWalls(ball);
                        }
                    }
                }
            }
            var len: number = this.ballArr.length;
            for (i = 0; i < len - 1; i++) {
                var ballA: NewBall = this.ballArr[i];
                if (ballA.isJindong != -1) {
                    continue;
                }
                for (var j: number = i + 1; j < len; j++) {
                    var ballB: NewBall = this.ballArr[j];
                    if (ballB.isJindong != -1) {
                        continue;
                    }
                    this.checkCollision(ballA, ballB);//碰撞检测 (第一个球)
                }
            }
            if (len == num) {
                this.isLoading = 0;
                this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this);
                this.slipsize = 0;//微调复位
                console.log("回合结束");
                if (this.isLianxi == 2) {
                    this.ld.visible = true;
                    this.slipgroup.visible = true;
                    this.MyRange.touchEnabled = true;
                }

                // this.seeAboutSurplus(this.getnowshootcolor(), this.ballArr);
                this.showGan(this.XX, this.YY, this.ballArr[this.isbai].nextX, this.ballArr[this.isbai].nextY);
                this.long = 0;
                if (this.isLianxi == 2)
                    this.UpdateLiduLabel();
                this.addGanEvt();
                for (var i: number = this.ballArr.length - 1; i >= 0; i--) {//重置所有球的属性
                    var b: NewBall = this.ballArr[i];
                    if (b.isJindong == -2) {
                        continue;
                    }
                    b.moc = 0.9997;
                    b.degree = 0;
                    b.isJindong = -1;
                }

                if (this.isLianxi == 1) {
                    /**球的数组 */
                    var tableArr: any[] = [];
                    for (var i: number = 0; i < this.ballArr.length; i++) {
                        this.ballArr[i].nextX = Math.round(this.ballArr[i].nextX);
                        this.ballArr[i].nextY = Math.round(this.ballArr[i].nextY);
                        tableArr.push(this.ballArr[i]);
                    }
                }
                else if (this.isLianxi == 2) {
                    let surplusNum = 0;
                    for (let i = 0; i < this.ballArr.length; i++) {
                        if (this.ballArr[i].isJindong != -2) {
                            surplusNum++;
                        }
                    }
                    if (surplusNum == 1) {
                        // var winUI: WinUI = new WinUI();
                        // console.log("solo win");
                        // winUI.x = this.centerX - 546 / 2;
                        // winUI.y = this.centerY - 469 / 2;

                        // this.addChild(winUI);
                        // winUI.playAction();
                    }
                    else {
                        this.isOwnGame = true;
                    }

                    if (this.ballArr[this.isbai].nextX == -17 && this.ballArr[this.isbai].nextY == -17) {
                        this.isBaiqiu = 2;
                    }
                }

                this.isMove = false;

                break;
            }
        }
        var len: number = this.ballArr.length;
        for (var i: number = len - 1; i >= 0; i--) {
            var b: NewBall = this.ballArr[i];
            if (b.isJindong == -2) {
                continue;
            }
            // setBallPos(b.nextX, b.nextY, b.lastX, b.lastY, i);

            this.AllGroup.$children[i].x = ball.nextX;
            this.AllGroup.$children[i].y = ball.nextY;
            b.lastX = b.nextX;
            b.lastY = b.nextY;
        }
    }

    private checkCollision(ball0: NewBall, ball1: NewBall) {
        var dx: number = ball1.nextX - ball0.nextX;
        var dy: number = ball1.nextY - ball0.nextY;
        var dist: number = Math.sqrt(dx * dx + dy * dy);
        if (dist < 2 * this._r) {

            var _vxx: number = ball0.vx + ball1.vx;
            var _vyy: number = ball0.vy + ball1.vy;

            var v = Math.sqrt(_vxx * _vxx + _vyy * _vyy) / 2;

            var angle: number = Math.atan2(dy, dx);
            var sin: number = Math.sin(angle);
            var cos: number = Math.cos(angle);

            var pos0: egret.Point = new egret.Point(0, 0);
            var pos1: egret.Point = this.rotateR(dx, dy, sin, cos, true);

            var vel0: egret.Point = this.rotateR(ball0.vx, ball0.vy, sin, cos, true);
            var vel1: egret.Point = this.rotateR(ball1.vx, ball1.vy, sin, cos, true);

            var overlap: number = 2 * this._r - Math.abs(pos0.x - pos1.x);

            pos0.x -= 0.5 * overlap + 0.0001;
            pos1.x += 0.5 * overlap + 0.0001;

            var pos0F: any = this.rotateR(pos0.x, pos0.y, sin, cos, false);
            var pos1F: any = this.rotateR(pos1.x, pos1.y, sin, cos, false);
            ball1.nextX = ball0.nextX + pos1F.x;
            ball1.nextY = ball0.nextY + pos1F.y;
            ball0.nextX = ball0.nextX + pos0F.x;
            ball0.nextY = ball0.nextY + pos0F.y;

            var vxTotal: number = vel0.x - vel1.x;
            vel0.x = vel1.x;
            vel1.x = vxTotal + vel0.x;

            var vel0F: any = this.rotateR(vel0.x, vel0.y, sin, cos, false);
            var vel1F: any = this.rotateR(vel1.x, vel1.y, sin, cos, false);
            ball0.vx = vel0F.x;
            ball0.vy = vel0F.y;
            ball1.vx = vel1F.x;
            ball1.vy = vel1F.y;

        }
    }


    private rebound(ball0: NewBall, _x, _y, num) {
        if (num == 0) {
            ball0.vx = 0;
            ball0.vy = 0;
            return;
        }
        var dx: number = _x - ball0.nextX;
        var dy: number = _y - ball0.nextY;
        var angle: number = Math.atan2(dy, dx);
        var sin: number = Math.sin(angle);
        var cos: number = Math.cos(angle);

        var pos0: egret.Point = new egret.Point(0, 0);
        var pos1: egret.Point = this.rotateR(dx, dy, sin, cos, true);
        pos0.x = pos1.x - this._r - 0.0001;
        var pos0F: any = this.rotateR(pos0.x, pos0.y, sin, cos, false);
        ball0.nextX = ball0.nextX + pos0F.x;
        ball0.nextY = ball0.nextY + pos0F.y;

        var vel0: egret.Point = this.rotateR(ball0.vx, ball0.vy, sin, cos, true);
        var vel1: egret.Point = this.rotateR(0, 0, sin, cos, true);
        var vxTotal: number = vel0.x - vel1.x;
        vel0.x = vel1.x;
        vel1.x = vxTotal + vel0.x;
        var vel0F: any = this.rotateR(vel0.x, vel0.y, sin, cos, false);
        var vel1F: any = this.rotateR(vel1.x, vel1.y, sin, cos, false);
        ball0.vx = num * (vel0F.x - vel1F.x);
        ball0.vy = num * (vel0F.y - vel1F.y);
    }
    private checkWalls(b: NewBall) {
        for (var i: number = 0; i < 6; i++) {
            var _hx: number = this.posArr[i].hx;
            var _hy: number = this.posArr[i].hy;
            if (b.nextX > _hx - this.holeR && b.nextX < _hx + this.holeR && b.nextY > _hy - this.holeR && b.nextY < _hy + this.holeR) {
                var dx: number = b.nextX - _hx;
                var dy: number = b.nextY - _hy;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this.holeR) {
                    console.log("ball: " + b.idx);
                    b.vx = 0;
                    b.vy = 0;
                    b.isJindong = i;
                }
            }
        }
        if (b.nextX < this.posArr[0].x2 + this._r) {
            if (b.nextY > this.posArr[0].y2 && b.nextY < this.posArr[3].y1) {
                b.nextX = this.posArr[0].x2 + this._r;
                b.vx *= this.bounce;
                return false;
            }
            if (b.nextY <= this.posArr[0].y2) {
                var dx: number = b.nextX - this.posArr[0].x2;
                var dy: number = b.nextY - this.posArr[0].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[0].x2, this.posArr[0].y2, 0.7);
                }
            }
            if (b.nextY >= this.posArr[3].y1) {
                var dx: number = b.nextX - this.posArr[3].x1;
                var dy: number = b.nextY - this.posArr[3].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[3].x1, this.posArr[3].y1, 0.7);
                }
            }
        }
        if (b.nextX > this.posArr[2].x2 - this._r) {
            if (b.nextY > this.posArr[2].y2 && b.nextY < this.posArr[5].y1) {
                b.nextX = this.posArr[2].x2 - this._r;
                b.vx *= this.bounce;
            }
            if (b.nextY <= this.posArr[2].y2) {
                var dx: number = b.nextX - this.posArr[2].x2;
                var dy: number = b.nextY - this.posArr[2].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[2].x2, this.posArr[2].y2, 0.7);
                }
            }
            if (b.nextY >= this.posArr[5].y1) {
                var dx: number = b.nextX - this.posArr[5].x1;
                var dy: number = b.nextY - this.posArr[5].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[5].x1, this.posArr[5].y1, 0.7);
                }
            }
        }
        if (b.nextY < this.posArr[0].y1 + this._r) {
            if (b.nextX > this.posArr[0].x1 && b.nextX < this.posArr[1].x1) {
                b.nextY = this.posArr[0].y1 + this._r;
                b.vy *= this.bounce;
            }
            if (b.nextX > this.posArr[1].x2 && b.nextX < this.posArr[2].x1) {
                b.nextY = this.posArr[0].y1 + this._r;
                b.vy *= this.bounce;
            }
            if (b.nextX <= this.posArr[0].x1) {
                var dx: number = b.nextX - this.posArr[0].x1;
                var dy: number = b.nextY - this.posArr[0].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[0].x1, this.posArr[0].y1, 0.7);
                }
            }
            if (b.nextX >= this.posArr[1].x1 && b.nextX < this.posArr[1].hx) {
                var dx: number = b.nextX - this.posArr[1].x1;
                var dy: number = b.nextY - this.posArr[1].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[1].x1, this.posArr[1].y1, 0.7);
                }
            }
            if (b.nextX <= this.posArr[1].x2 && b.nextX > this.posArr[1].hx) {
                var dx: number = b.nextX - this.posArr[1].x2;
                var dy: number = b.nextY - this.posArr[1].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[1].x2, this.posArr[1].y2, 0.7);
                }
            }
            if (b.nextX >= this.posArr[2].x1) {
                var dx: number = b.nextX - this.posArr[2].x1;
                var dy: number = b.nextY - this.posArr[2].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[2].x1, this.posArr[2].y1, 0.7);
                }
            }
        }
        if (b.nextY > this.posArr[3].y2 - this._r) {
            if (b.nextX > this.posArr[3].x2 && b.nextX < this.posArr[4].x1) {
                b.nextY = this.posArr[3].y2 - this._r;
                b.vy *= this.bounce;
            }
            if (b.nextX > this.posArr[4].x2 && b.nextX < this.posArr[5].x2) {
                b.nextY = this.posArr[3].y2 - this._r;
                b.vy *= this.bounce;
            }
            if (b.nextX <= this.posArr[3].x2) {
                var dx: number = b.nextX - this.posArr[3].x2;
                var dy: number = b.nextY - this.posArr[3].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[3].x2, this.posArr[3].y2, 0.7);
                }
            }
            if (b.nextX >= this.posArr[4].x1 && b.nextX < this.posArr[4].hx) {
                var dx: number = b.nextX - this.posArr[4].x1;
                var dy: number = b.nextY - this.posArr[4].y1;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[4].x1, this.posArr[4].y1, 0.7);
                }
            }
            if (b.nextX <= this.posArr[4].x2 && b.nextX > this.posArr[4].hx) {
                var dx: number = b.nextX - this.posArr[4].x2;
                var dy: number = b.nextY - this.posArr[4].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[4].x2, this.posArr[4].y2, 0.7);
                }
            }
            if (b.nextX >= this.posArr[5].x2) {
                var dx: number = b.nextX - this.posArr[5].x2;
                var dy: number = b.nextY - this.posArr[5].y2;
                var dist: number = Math.sqrt(dx * dx + dy * dy);
                if (dist < this._r) {
                    this.rebound(b, this.posArr[5].x2, this.posArr[5].y2, 0.7);
                }
            }
        }
    }



}


